<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="账户ID" prop="id">
            <el-input v-model="queryParams.id" placeholder="请输入账户ID" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="账户名称" prop="accName">
            <el-input v-model="queryParams.accName" placeholder="请输入账户名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleStat" v-hasPermi="['retail:account:list']">查看统计</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="accountList" class="tableEmpty">
        <el-table-column label="账户ID" align="center" prop="id" />
        <el-table-column label="账户名称" align="center" prop="accName" />
        <el-table-column label="类型" align="center" prop="accType" />
        <el-table-column label="账户余额" align="right" prop="balance">
          <template #default="scope">
            <el-link type="success">{{ scope.row.balance/100 }}</el-link>
          </template>
        </el-table-column>
        <el-table-column label="不可用余额" align="right" prop="unBalance">
          <template #default="scope">
            <el-link type="info">{{ scope.row.unBalance/100 }}</el-link>
          </template>
        </el-table-column>
        <el-table-column label="冻结金额" align="right" prop="frozeAmount">
          <template #default="scope">
            <el-link type="danger" size="20">{{ scope.row.frozeAmount/100 }}</el-link>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </el-card>

    <el-dialog :title="statDialog.title" v-model="statDialog.visible" width="900px" append-to-body>
      <AccountStat v-if="statDialog.visible" />
    </el-dialog>
  </div>
</template>

<script setup name="Account" lang="ts">
import { listAccount, getAccount } from '@/api/retail/account';
import { AccountVO, AccountQuery, AccountForm } from '@/api/retail/account/types';
import AccountStat from "@/views/retail/account/stat.vue";

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_normal_disable } = toRefs<any>(proxy?.useDict("sys_normal_disable"));

const accountList = ref<AccountVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const accountFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: AccountForm = {
  id: undefined,
  user: undefined,
  accName: undefined,
  accType: undefined,
  balance: undefined,
  unBalance: undefined,
  frozeAmount: undefined,
  status: undefined,
}
const data = reactive<PageData<AccountForm, AccountQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    user: undefined,
    accName: undefined,
    accType: undefined,
    balance: undefined,
    unBalance: undefined,
    frozeAmount: undefined,
    status: undefined,
    params: {
    }
  },
  rules: {
  }
});

const statDialog = reactive<DialogOption>({
  visible: false,
  title: ''
});
const { queryParams, form } = toRefs(data);

/** 查询资金账号列表 */
const getList = async () => {
  loading.value = true;
  const res = await listAccount(queryParams.value);
  accountList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  accountFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 导出按钮操作 */
const handleStat = () => {
  statDialog.visible = true;
  statDialog.title = "资金统计信息";
}

onMounted(() => {
  getList();
});
</script>
